package threads;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.LockSupport;

public class TestTaobaoThread2s_LockSupport {
    private final static TestTaobaoThread2s_LockSupport t = new TestTaobaoThread2s_LockSupport();
    private volatile List<String> list = Collections.synchronizedList(new ArrayList<>());
    static Thread t1,t2;

    public void add(String o) {
        list.add(o);
    }

    public int size() {
        return list.size();
    }

    public static void main(String[] args) {
        t2 = new Thread(() -> {
            LockSupport.park();
            LockSupport.unpark(t1);
            System.out.println("t2 end.");
        }, "t2");
        t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                t.add(String.valueOf(i));
                System.out.println("add " + i);
                if (i == 5) {
                    LockSupport.unpark(t2);
                    LockSupport.park();
                }
            }
        }, "t1");
        t1.start();
        t2.start();
    }
}
